﻿//-----------------------------------------------------------------------
// <copyright file="SpeakCommand.cs" company="AFS">
// Copyright (c) AFS. All rights reserved.
//
//    This file is part of Chatbox.
//
//    Chatbox is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    any later version.
//
//    Chatbox is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with Chatbox. If not, see http://www.gnu.org/copyleft/lesser.html
// </copyright>
//----------------------------------------------------------------------- 

namespace ChatBox.UI.Windows.Main
{
    using ChatBox.DomainObjects.ResultData;
    using ChatBox.Interfaces;
    using ChatBox.UI.Commands;
    using ChatBox.Utility;

    /// <summary>
    /// Command to send the message text
    /// </summary>
    public class SpeakCommand : GenericCommandBase<MainWindowViewModel>
    {
        /// <summary>
        /// The service to use to send messages
        /// </summary>
        private readonly IChatMessageSend sendService;

        /// <summary>
        /// The view model to notify afterwards
        /// </summary>
        private MainWindowViewModel currentViewModel;

        /// <summary>
        /// Initializes a new instance of the SpeakCommand class
        /// </summary>
        /// <param name="sendService">The service to use to send messages</param>
        public SpeakCommand(IChatMessageSend sendService)
        {
            this.sendService = sendService;
            this.sendService.Completed += this.MessageSendCompleted;
        }

        /// <summary>
        /// IS there something to say?
        /// </summary>
        /// <param name="value">the main window view model</param>
        /// <returns>true id the user can speak</returns>
        public override bool CanExecuteOnValue(MainWindowViewModel value)
        {
            if (value == null)
            {
                return false;
            }

            return value.Speak.SpeakMessageText.HasContent();
        }

        /// <summary>
        /// Execute the speak command
        /// </summary>
        /// <param name="value">the view model</param>
        public override void ExecuteOnValue(MainWindowViewModel value)
        {
            if (value == null)
            {
                return;
            }

            this.currentViewModel = value;

            // check that basic login setup is in place
            if (!value.CheckNameAndPassword("speak"))
            {
                return;
            }

            string messageText = value.Speak.SpeakMessageText;

            // check that there's something to send
            if (messageText.IsNullEmptyOrWhiteSpace())
            {
                return;
            }

            messageText = messageText.Trim();

            // send it
            this.sendService.SendMessage(messageText);

            // store it in case it's needed back
            value.Speak.LastMessage = messageText;

            // reset the Ui
            value.Speak.SpeakMessageText = string.Empty;
        }

        /// <summary>
        /// Send completed handler
        /// </summary>
        /// <param name="sender">the event sender</param>
        /// <param name="e">the vent params</param>
        private void MessageSendCompleted(object sender, EventArgs<OperationResult> e)
        {
            // update the main window view model 
            this.currentViewModel.EngineOperationCompleted(sender, e);
        }
    }
}
